<?php
class SignupController extends Controller{


	public function index($params){
		$this->getSmarty();
		$this->smarty->assign ( 'pagesets', $this->headerDatas() );

		//将邀请人code写入session中
		$invaitecode = isset ( $_GET ['i'] ) ?  $_GET ['i']  : null;
		if (! empty ( $invaitecode )) {
			$_SESSION['invaiteid'] = $invaitecode;
		}

		if(!CommonBase::isPost()){
			$this->smarty->display("user_center/register.tpl");
			return;
		}
		$this->smarty->display("user_center/register.tpl");
	}

	public function register(){
		$email = $_POST ["email"];
		$invaiteid = $_SESSION['invaiteid'];
		$this->getSmarty();
		require_once SERVICE . DS . 'admin' . DS . 'UsersService.class.php';
		$userService = new UsersService( $this->getDB());
		$user = $userService->getEmailByName($email);
		if(!empty($user->email)){
			$info = array (
				"type" => "error",
				"info" => "User already exists, cannot repeat registration"
				);
				echo json_encode($info);
				return ;
		}

		$emailArr = explode ( "@", $email );
		$uid = uniqid ();
		$user = array (
				'userid' => $uid,
				'login_type' => 0,
				'username' => $emailArr [0],
				'password' => md5 ( $_POST ["password"] ),
				'email' => $_POST ["email"],
				'user_invite_code' => $uid,
				'invite_userid' => $invaiteid,
				'points' =>200
		);
		$userService->addUser ( $user );
		//为邀请人补充相关的信息
		self::inviteMsgUpdate($invaiteid,$user);

		//添加用户的缓存数据
		$userinfo = array(
        		'userid' => $user['userid'],
	        	'username' => $user['username'],
	            'email' => $user['email'],
        		'points' => $user['points']
		);
		$_SESSION['user'] = $userinfo;

		require_once COMMON.DS.'SendMailUtil.class.php';
		SendMailUtil::sendmail("Welcome to register babyspeedy.com",$email.",Registered successfully",$email);

		$info = array (
				"type" => "success",
				"info" => "Registered successfully"
				);
				echo json_encode($info);
	}
	/**
	 *
	 * 判断是否有邀请人信息..如果有.
	 * 1:为邀请人添加100的积分奖励
	 * 2:添加邀请人积分添加记录
	 * 3:添加邀请人邀请记录表.状态为注册,已返利
	 * @param unknown_type $invaiteid
	 */
	public function inviteMsgUpdate($invaiteid,$user){
		if(empty($invaiteid)){
			return;
		}
		require_once SERVICE . DS . 'CommonDataService.class.php';
		$service = new CommonDataService ( $this->getDB () );
		//添加对应用户的邀请记录数据

		//邀请人信息
		$inviteuser = $service->getResult("select * from users where userid='".$invaiteid."'");
		if($inviteuser == null){
			return;
		}
		//判断该注册用户是否已经被邀请了..如果被邀请了.则更新记录的invite_user_id,
		//还有注册返利状态,否则直接添加一条新的纪录信息
		//判断当前邮件是否被邀请过
		$query = "select * from user_invite_record where userid = '". $inviteuser->userid."' and invite_user_mail= '".$user['email']."'";
		$result = $service->getResult($query);
		if($result == null){
			//添加新的纪录信息
			$userInviteRecord = array (
					'id' => uniqid (),
					'userid' => $inviteuser->userid,
					'username' => $inviteuser->username,
					'invite_user_id' => $user['userid'],
					'invite_user_mail' => $user['email'],
					'state' => 1,
					'rebate'=>1,
					'create_time' =>date("Y-m-d h:m:s")
			);
			$service->add("user_invite_record",$userInviteRecord);
		}else{
			$data = array (
					'invite_user_id' => $user['userid'],
					'state' => 1,
					'rebate'=>1 
			);
			$conditions = array(
					'id' => $result->id
			);
			$service->edit("user_invite_record", $data, $conditions);
		}
		
		//更新邀请者的积分,添加100积分奖励
		$service->exSql("update users set points=points+100 where userid='".$invaiteid."'");
		//为邀请者添加一条积分修改记录
		$invitePointsRecord = array (
					'id' => uniqid (),
					'userid' => $inviteuser->userid,
					'username' => $inviteuser->username,
					'record_type' => 0,
					'points' => 100,
					'points_type' => 1,
					'points_desc'=>"Invite mail ".$user['email'],
					'create_time' =>date("Y-m-d h:m:s")
			);
			$service->add("user_points_record",$invitePointsRecord);
	}
	public function handreg(){
		$this->getSmarty();
	}
	public function userExsist($msg){
		return "<div class='alert alert-error'>$msg</div>";
	}
	public function userSuccess($msg){
		return "<div class='alert alert-success'>$msg</div>";
	}
	public function headerDatas(){
		require_once COMMON . DS  . 'Constant.class.php';
		$js = array(WEBSITE_URL."public/operation/js/signup.js", WEBSITE_URL."public/assets/js/jquery.validationEngine-zh.js",WEBSITE_URL."public/assets/js/jquery.validationEngine.js");
		$css = array(WEBSITE_URL."public/assets/css/validationEngine.jquery.css");
		$pagesets = array("seo"=>Constant::$seoarray,"js"=>$js,"css"=>$css,"nav"=>Constant::getNav());
		return $pagesets;
	}
}